<?php
defined("_JEXEC") or die("Restricted access");

class JoomlaIniTranslatorControllerTranslator extends JoomlaIniTranslatorController
{	
	function add() {
		JRequest::setVar("view", "translator");
		JRequest::setVar("layout", "form");
		JRequest::setVar("hidemainmenu", 1);
		
		parent::display();
	}
	
	function save() {
		$model = $this->getModel('joomlainitranslator');
		
		$srcLangDir = JRequest::getVar("srcLangDir");
		$tarLangDir = JRequest::getVar("tarLangDir");
		// 确认是否重复
		// 重复条件：language dir
		$db = $model->getDBO();
		$query = "SELECT * FROM #__joomlainitranslator WHERE srcLangDir = '{$srcLangDir}' AND tarLangDir = '{$tarLangDir}'";
		$db->setQuery($query);
		$res = $db->loadObjectList();
		if (count($res) > 0) {
			$msg = JText::_("Failed to add Translator, Source Language Directory : {$srcLangDir}; Target Language Diretory : {$tarLangDir} already existed!<br />".$model->getError());
			$type = "error";
			$url = 'index.php?option=com_joomlainitranslator';
			$this->setRedirect($url, $msg, $type);
			$this->redirect();
		}

		if ($model->store()) {
			$msg = JText::_("Translator add success!");
			$type = "message";
		} else {
			$msg = JText::_("Failed to add Translator, some thing trouble!<br />".$model->getError());
			$type = "error";
		}
		
		$url = 'index.php?option=com_joomlainitranslator';
		$this->setRedirect($url, $msg, $type);
		$this->redirect();
	}
	
	function remove() {
		$model = $this->getModel('joomlainitranslator');
		if(!$model->delete()) {
			$msg = JText::_( 'Error: One or More Translator Could not be Deleted' );
		} else {
			$msg = JText::_( 'Greeting(s) Deleted' );
		}

		$this->setRedirect( 'index.php?option=com_joomlainitranslator', $msg );
		$this->redirect();
	}
	
	function cancel() {
		$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=com_joomlainitranslator', $msg );
		$this->redirect();
	}
	
	function runTranslator() {		
		JRequest::setVar("view", "run");
		JRequest::setVar("layout", "run");
		JRequest::setVar("hidemainmenu", 1);
		
		parent::display();
		//var_dump($translatorInfo); die;
	}
}